package com.sample.springbatch.job.sample.processor;

import com.sample.springbatch.model.User;
import com.sample.springbatch.repository.RepayOrderInfoRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by zwg on 2017/7/20.
 */
public class UserItemProcessor implements ItemProcessor<User,User> {

    private static final Logger log = LoggerFactory.getLogger(UserItemProcessor.class);

    @Autowired
    RepayOrderInfoRepository repayOrderInfoRepository;

    @Override
    public User process(final User user) throws Exception {
        final String firstName = user.getFirstName().toUpperCase();
        final String lastName = user.getLastName().toUpperCase();

        final User transformedUser = new User(firstName, lastName);

        log.info("Converting (" + user + ") into (" + transformedUser + ")");

        Long time1 = System.currentTimeMillis();
        for(int i=0; i<10000; i++){

            repayOrderInfoRepository.findByUserId("10000000012");
            repayOrderInfoRepository.findByUserId("10000000011");
            repayOrderInfoRepository.findByUserId("10000000002");
            if(i%10==0){
                Long time2 = System.currentTimeMillis();
                System.out.println(i+" time:"+(time2-time1));
                time1=time2;
            }
        }

        return transformedUser;
    }
}
